﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;

namespace IHISCETeim.DAL
{
    /// <summary>
    /// 岗位培训
    /// </summary>
    public class XZJobTrainDAL
    {
        /// <summary>
        /// 获取所有的岗位信息
        /// </summary>
        /// <returns></returns>
        public static IDataReader GetAllXZJobList()
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("XZJobList_GetAllXZJobList");
            return DB.ExecuteReader(dbCommand);
	    }
        /// <summary>
        /// 添加岗位培内容
        /// </summary>
        /// <param name="JobId"></param>
        /// <param name="TrainId"></param>
        /// <param name="ClassHour"></param>
        /// <param name="Unit"></param>
        /// <returns></returns>
        public static bool AddXZJobTrain(int JobId,List<XZJobTrainInfo> list)
        {
            Database DB = DatabaseFactory.CreateDatabase();
            using (DbConnection conn = DB.CreateConnection())
            {
                //打开连接
                conn.Open();
                //创建事务
                DbTransaction trans = conn.BeginTransaction();
                try
                {

                    DbCommand dbCommanddel = DB.GetStoredProcCommand("XZJobTrain_DeleteXZJobTrain", JobId);
                    DB.ExecuteScalar(dbCommanddel);

                    foreach (XZJobTrainInfo obj in list)
                    {
                        DbCommand dbCommand = DB.GetStoredProcCommand("XZJobTrain_AddXZJobTrain", obj.JobId, obj.TrainId, obj.ClassHour, obj.Unit);
                         DB.ExecuteScalar(dbCommand);
                    }

                    trans.Commit();
                    return true;
                }
                catch (Exception exc)
                {
                    //发生异常，事务回滚
                    trans.Rollback();
                    return false;
                }
                finally
                {
                    //关闭连接
                    conn.Close();
                }
            }


            
        }
        
        /// <summary>
        /// 获取岗位对应的所有培训内容
        /// </summary>
        /// <param name="JobId"></param>
        /// <returns></returns>
        public static IDataReader GetAllByJobId(int JobId)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("XZJobTrain_GetAllByJobId", JobId);
            return DB.ExecuteReader(dbCommand);
        }

        /// <summary>
        /// 获取岗位对应的所有培训内容,用于导出
        /// </summary>
        /// <param name="JobId"></param>
        /// <returns></returns>
        public static DataTable GetAllByJobId2(int JobId)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("XZJobTrain_GetAllByJobId", JobId);
            return DB.ExecuteDataSet(dbCommand).Tables[0];
        }
        /// <summary>
        /// 添加培训内容清单
        /// </summary>
        /// <param name="TrainDept"></param>
        /// <param name="TrainContent"></param>
        /// <param name="ClassHour"></param>
        /// <param name="Unit"></param>
        /// <param name="ShowIndex"></param>
        /// <returns></returns>
        public static int AddXZTrainList(XZTrainListInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("XZTrainList_AddXZTrainList", obj.TrainDept, obj.TrainContent, obj.ClassHour, obj.Unit, obj.ShowIndex);
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }
        /// <summary>
        /// 删除培训内容清单
        /// </summary>
        /// <param name="TrainId"></param>
        /// <returns></returns>
        public static bool DeleteXZTrainList(int TrainId)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("XZTrainList_DeleteXZTrainList", TrainId);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 根据条件获取培训内容清单
        /// </summary>
        /// <param name="sqlwhere"></param>
        /// <returns></returns>
        public static IDataReader GetAllXZTrainList(string sqlwhere)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("XZTrainList_GetAllXZTrainList", sqlwhere);
            return DB.ExecuteReader(dbCommand);
        }
        /// <summary>
        /// 根据ID单个获取培训内容清单
        /// </summary>
        /// <param name="TrainId"></param>
        /// <returns></returns>
        public static IDataReader GetXZTrainById(int TrainId)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("XZTrainList_GetXZTrainById", TrainId);
            return DB.ExecuteReader(dbCommand);
        }
        /// <summary>
        /// 更新培训内容清单
        /// </summary>
        /// <param name="TrainId"></param>
        /// <param name="TrainDept"></param>
        /// <param name="TrainContent"></param>
        /// <param name="ClassHour"></param>
        /// <param name="Unit"></param>
        /// <param name="ShowIndex"></param>
        /// <returns></returns>
        public static bool UpdateXZTrainList(XZTrainListInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("XZTrainList_UpdateXZTrainList", obj.TrainId, obj.TrainDept, obj.TrainContent, obj.ClassHour, obj.Unit, obj.ShowIndex);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }

        public static DataTable XZTrainList_GetAllDept()
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("XZTrainList_GetAllDept");
            return DB.ExecuteDataSet(dbCommand).Tables[0];
        }
    }
}
